package com.hundsun.yr.universal.p2p.ui.activity.base;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppCompatActivity;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.utils.ThemeUtils;
import com.hundsun.yr.universal.p2p.HsApplication;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.datahandler.BaseDataHandler;
import com.hundsun.yr.universal.p2p.ui.activity.GestureLockLoginActivity;
import com.hundsun.yr.universal.p2p.ui.view.ProgressDailog;
import com.hundsun.yr.universal.p2p.view.base.BaseView;

/**
 * @Title: BaseActivity.java
 * @Description: Activity基础类
 * @date 2016-1-13 下午6:41:58
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 * @version V1.0
 */
public abstract class BaseActivity extends BaseAppCompatActivity implements BaseView {

	/** 标题栏 **/
	protected Toolbar mToolbar;
	/** 标题 **/
	/** 标题栏menu文字 **/
	protected TextView toolbar_title, toolbar_menu;

	/** 加载进度条 **/
	protected ProgressDailog progressDailog = null;

	protected BaseDataHandler mDataHandler;
	/** 用户SharedPreferences **/
	public HSSharedPreferences sharedPreferences = null;
	public HSSharedPreferences mConfigSP = null;


	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		if (isApplyKitKatTranslucency()) {
//			setSystemBarTintDrawable(getResources().getDrawable(ThemeUtils.getStatusbar_color()));
//		}
	}

	/**
	 * 设置标题
	 * 
	 * @param res
	 */
	public void setToolbarTitle(int res) {
		if (null != toolbar_title) {
			toolbar_title.setText(res);
		}
	}

	/**
	 * 设置标题
	 * 
	 * @param titlle
	 */
	public void setToolbarTitle(String titlle) {
		if (null != toolbar_title) {
			toolbar_title.setText(titlle);
		}
	}

	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		mToolbar = findView(R.id.common_toolbar);
		if (null != mToolbar) {
			toolbar_title = findView(R.id.toolbar_title);
			toolbar_menu = findView(R.id.toolbar_menu);
			setSupportActionBar(mToolbar);
			getSupportActionBar().setTitle("");
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
		progressDailog = new ProgressDailog(this, R.style.progress_dialog);
		sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
		mConfigSP = new HSSharedPreferences(mContext, Config.SPPath.file_configInfo);
	}

	@Override
	protected int configTheme() {
		// TODO Auto-generated method stub
		// int theme = ThemeUtils.themeArr[ThemeUtils.getThemeColorIndex()][0];
		int theme = ThemeUtils.getTheme();
		if (theme > 0)
			return theme;
		return super.configTheme();
	}

	

	@Override
	protected void onResume() {
		super.onResume();
		// MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
//	        MobclickAgent.onPause(this);
	}
	/**
	 * 是否跳转到手势密码界面
	 * @return
	 */
	public boolean ifGoGeture(){
		if(sharedPreferences.getBoolean(UserInfo.ISLOGIN) && mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE)){
			readyGo(GestureLockLoginActivity.class);
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 是否跳转到指纹识别页面
	 * @return
     */
	public boolean ifGoFingerprintLockActivity(){
		if(sharedPreferences.getBoolean(UserInfo.ISLOGIN) && mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_FINGERPRINT)){
			readyGo(GestureLockLoginActivity.class);
			return true;
		}else{
			return false;
		}
	}
	
	@Override	
	protected void onStop() {
		super.onStop();
		BaseAppManager instance = BaseAppManager.getInstance();
		if(!instance.isAppOnForeground(this)){
			HsApplication.goGesture = true;
		}
	 }

	protected HsApplication getBaseApplication() {
		return (HsApplication) getApplication();
	}

	@Override
	public void showError(String msg, OnClickListener onClickListener) {
		toggleShowError(true, msg, onClickListener);
	}

	@Override
	public void showNetError(OnClickListener onClickListener) {
		toggleNetworkError(true, onClickListener);
	}

	@Override
	public void showLoading(String msg) {
		toggleShowLoading(true, null);
	}

	@Override
	public void hideLoading() {
		// toggleShowLoading(false, null);
		toggleHide();
	}

	@Override
	public void showEmpty(int drawableRes, String msg, OnClickListener onClickListener) {
		// TODO Auto-generated method stub
		toggleShowEmpty(true, drawableRes, msg, onClickListener);
	}

	@Override
	public void showProgress(String msg) {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(msg)) {
			progressDailog.showProgress();
		} else {
			progressDailog.showProgress(msg);
		}

	}

	@Override
	public void closeProgress() {
		// TODO Auto-generated method stub

		if (progressDailog != null && progressDailog.isShowing()) {
			progressDailog.closeProgress();
		}
	}

	/**
	 * 是否改变状态栏背景
	 * 
	 * @return
	 */
	protected abstract boolean isApplyKitKatTranslucency();

}
